package com.woniu.permissions.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu.model.Result;
import com.woniu.permissions.form.AddAccountRoleForm;
import com.woniu.permissions.form.UpdateAccountRoleForm;
import com.woniu.permissions.model.Account;
import com.woniu.permissions.model.AccountRole;
import com.woniu.permissions.model.Role;
import com.woniu.permissions.param.AddAccountRoleParam;
import com.woniu.permissions.param.UpdateAccountRoleParam;
import com.woniu.permissions.service.AccountRoleService;
import com.woniu.util.RedisUtil;
import com.woniu.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@RestController
@RequestMapping("/accountRole")
@Api(tags = "账号角色接口")
public class AccountRoleController {
    @Resource
    private AccountRoleService accountRoleService;
    @Resource
    private RedisUtil redisUtil;
    /**
     * 新增账号角色关联
     * @param form 账号id 角色id
     * @return
     */
    @ApiOperation("新增账号角色关联")
    @PostMapping("/addAccountRole")
    public Result<AccountRole> addAccountRole(@RequestBody AddAccountRoleForm form,@RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        JwtUtil.parseAdminToken(refreshToken);
        AccountRole accountRole = accountRoleService.addAccountRole(BeanUtil.toBean(form, AddAccountRoleParam.class));
        return Result.newInstance(AccountRole.class).setData(accountRole).setMessage("新增成功");
    }
    /**
     * 修改账号角色关联
     * @param form 账号id 角色id
     * @return
     */
    @ApiOperation("新增账号角色关联")
    @PostMapping("/updateAccountRole")
    public Result<AccountRole> updateAccountRole(@RequestBody UpdateAccountRoleForm form,@RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        JwtUtil.parseAdminToken(refreshToken);
        AccountRole accountRole = accountRoleService.updateAccountRole(BeanUtil.toBean(form, UpdateAccountRoleParam.class));
        return Result.newInstance(AccountRole.class).setData(accountRole).setMessage("新增成功");
    }
    /**
     * 查询账号角色关联
     * @param accountId 账号id
     * @return
     */
    @ApiOperation("新增账号角色关联")
    @GetMapping("/selectAccountRole/{accountId}")
    public Result<AccountRole> selectAccountRole(
            @ApiParam(name = "accountId",value = "账号id")
            @PathVariable("accoutnId")
            String accountId,@RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        JwtUtil.parseAdminToken(refreshToken);
        AccountRole accountRole = accountRoleService.selectAccountRole(accountId);
        return Result.newInstance(AccountRole.class).setData(accountRole).setMessage("新增成功");
    }

    /**
     * 根据账号id查询角色
     * @param accountId
     * @return
     */
    @ApiOperation("根据账号id查询角色")
    @GetMapping("/selectRoleByAccountId/{accountId}")
    public Result<Role> selectRoleByAccountId(
            @PathVariable("accountId")
            @ApiParam(name = "accountId",value = "账号id")
            String accountId
    ){
        Role roleName = accountRoleService.selectRoleByAccountId(accountId);
        return Result.newInstance(Role.class).setData(roleName).setMessage("根据账号id查询角色成功");
    }
}

